package com.hxgz.steel.controller.backend;

import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.NavigationManager;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.dto.DropValueDto;
import com.hxgz.steel.service.NavigationManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(value = "NavigationManagerController", description = "后台:底部导航管理")
@RestController
@RequestMapping("navigationManager")
public class NavigationManagerController {

  @Autowired
  NavigationManagerService navigationManagerService;

  @ApiOperation("查询")
  @RequestMapping(value = "check.do", method = RequestMethod.POST)
  public ServerResponse check(
      @ApiParam("从属服务 1买家服务 2卖家服务 3售后服务 4购物指南 5金融服务 6关于我们 7用户协议") @RequestParam(required = false) int status,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    List<NavigationManager> navigationManagers = new ArrayList<>();
    if (status == 1) {
      navigationManagers = navigationManagerService.getBuyerServiceList();
    } else if (status == 2) {
      navigationManagers = navigationManagerService.getSellerServiceList();
    } else if (status == 3) {
      navigationManagers = navigationManagerService.getAfterSellerServiceList();
    } else if (status == 4) {
      navigationManagers = navigationManagerService.getShopServiceList();
    } else if (status == 5) {
      navigationManagers = navigationManagerService.getMoneyServiceList();
    } else {
      navigationManagers = navigationManagerService.getAboutUsList();
    }
    return ServerResponse.createBySuccess(navigationManagers);
  }

  @ApiOperation("修改内容")
  @RequestMapping(value = "update.do", method = RequestMethod.POST)
  public ServerResponse update(@RequestParam String id, @ApiParam("标题") String title,
      @ApiParam("内容") String content, HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return navigationManagerService.update(id, title, content);
  }

  @ApiOperation("修改状态")
  @RequestMapping(value = "updateStatus.do", method = RequestMethod.POST)
  public ServerResponse updateStatus(@RequestParam String id,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return navigationManagerService.updateStatus(id);
  }

  @ApiOperation("根据id查询")
  @RequestMapping(value = "checkId.do", method = RequestMethod.POST)
  public ServerResponse checkId(@RequestParam String id, HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return navigationManagerService.checkId(id);
  }

  @ApiOperation("导航列表")
  @RequestMapping(value = "getList.do",method = RequestMethod.POST)
  public ServerResponse getList(HttpServletRequest request){
    User user = Const.checkBackUserLogin(request);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
    }
    List<DropValueDto> list = navigationManagerService.getDropDownList();
    return ServerResponse.createBySuccess(list);
  }
}
